package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.huaben.UnionHuaBenAttackDamageData;
import com.yanqu.road.entity.union.huaben.UnionHuaBenUserAttack;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_UNION_HUA_BEN_DAMAGE_DETAIL, desc = "关卡伤害明细")
public class CrossUnionHuaBenDamageDetailCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionHuaBenProto.UnionHuaBenDamageDetailReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenDamageDetailReqMsg.parseFrom(packet.getBytes());
        int level = reqMsg.getLevel();
        int targetId = reqMsg.getTargetId();

        UnionHuaBenProto.UnionHuaBenDamageDetailRespMsg.Builder respMsg = UnionHuaBenProto.UnionHuaBenDamageDetailRespMsg.newBuilder();
        respMsg.setLevel(level);
        respMsg.setTargetId(targetId);
        int weekId = CrossUnionHuaBenConfigMgr.getThisWeekId();

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();
        if (context.getRet() != 0) {
            respMsg.setRet(context.getRet());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_DAMAGE_DETAIL, respMsg));
        }

        UnionHuaBenAttackDamageData damageData = CrossUnionHuaBenMgr.getUnionHuaBenAttackDamageData(weekId, context.getUnionInfo().getServerId(), context.getUnionInfo().getUnionUid(), level);
        if (damageData != null) {

            for (Map.Entry<Integer, List<UnionHuaBenUserAttack>> entry : damageData.getDamageMap().entrySet()) {
                if (entry.getKey() != targetId) {
                    continue;
                }
                for (int i = 0; i < entry.getValue().size(); i++) {
                    UnionHuaBenUserAttack tempData = entry.getValue().get(i);
                    UnionHuaBenProto.UnionHuaBenDamageDetailTemp.Builder builder = UnionHuaBenProto.UnionHuaBenDamageDetailTemp.newBuilder();
                    builder.addAllWave(tempData.getKillWaveList());
                    builder.setDamage(tempData.getDamage());
                    builder.setUserId(tempData.getUserId());
                    CrossUserBaseInfo userBaseInfo = CrossUserMgr.getCrossUserBaseInfo(tempData.getUserId());
                    if (userBaseInfo != null) {
                        builder.setPlayerBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo.getUserBaseInfo()));
                    }
                    respMsg.addDetail(builder);
                }

            }
        }

        respMsg.setRet(0);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_DAMAGE_DETAIL, respMsg));
    }

    @Override
    public void sendData() {

    }
}
